/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bankingsystem;


import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

/**
 * Defines the persistent Bank object in which all the details of the
 * bank are stored, in the form of Hashtables.
 * @author Gokul
 * @version  1.0
 */
public class Bank implements Serializable{

    /*Hashtables which store objects from each segment of the banking system. The key
    for each element in the hashtable is the unique identifier of that element.*/
    private Hashtable<String, Customer> customers;
    private Hashtable<String, FixedAccount> fixedAccounts;
    private Hashtable<String, CurrentAccount> currentAccounts;
    private Hashtable<String, SavingsAccount> savingsAccounts;
    private Hashtable<String, Loan> loans;
    private Hashtable<String, Staff> staff;

    /**
     * Template for creating a new Bank instance
     */
    Bank(){
        /*initializing variables*/
        customers = new Hashtable<String, Customer>();
        fixedAccounts = new Hashtable<String, FixedAccount>();
        currentAccounts = new Hashtable<String, CurrentAccount>();
        savingsAccounts = new Hashtable<String, SavingsAccount>();
        loans = new Hashtable<String, Loan>();
        staff = new Hashtable<String, Staff>();

        /*creating administrator*/
        Staff admin = new Staff("Admin", "883611535V","admin","root","administrator");
        this.addStaff(admin);
        /*creating sample customers*/
        Customer cust1 = new Customer("John Doe", "C001", "Boston, MA", 800112345, "010192", "first");
        this.addCustomer(cust1);
        Customer cust2 = new Customer("Bob Fisher", "C002", "New York, NY", 800567345, "010193", "second");
        this.addCustomer(cust2);

        /*creating sample fixed account*/
        FixedAccount f = new FixedAccount("F001", cust1, 12000, Calendar.getInstance(), 100, 12);
        this.addFixedAccount(f);


        /*creating sample current account*/
        CurrentAccount c = new CurrentAccount("C001",cust1,8000,Calendar.getInstance(), 1000);
        this.addCurrentAccount(c);

        /*creating sample savings account*/
        SavingsAccount s = new SavingsAccount("S001", cust2, 5000,Calendar.getInstance(), 10);
        this.addSavingsAccount(s);

        /*creating sample loan*/
        Loan l = new Loan(101, 1000, cust2, 8, new Date(), 50);
        this.addLoan(l);
    }

    /**
     * getter methods for the private variables. No need for setter methods because
     * the elements of the Hashtables are accessed individually rather than as the
     * whole Hashtable itself.
     */

    /**
     * @return the customers
     */
    public Hashtable<String, Customer> getCustomers() {
        return customers;
    }
    /**
     * @return the fixedAccounts
     */
    public Hashtable<String, FixedAccount> getFixedAccounts() {
        return fixedAccounts;
    }
    /**
     * @return the currentAccounts
     */
    public Hashtable<String, CurrentAccount> getCurrentAccounts() {
        return currentAccounts;
    }
    /**
     * @return the savingsAccounts
     */
    public Hashtable<String, SavingsAccount> getSavingsAccounts() {
        return savingsAccounts;
    }
    /**
     * @return the loans
     */
    public Hashtable<String, Loan> getLoans() {
        return loans;
    }
    /**
     * @return the staff
     */
    public Hashtable<String, Staff> getStaff() {
        return staff;
    }
    


    /**
     * Method for adding a new Customer object to the customers Hashtable
     * @param c the Customer object that needs to be added
     */
    public void addCustomer(Customer c){
        this.customers.put(c.getCustomerID(), c);
        new PersistenceManager().savedata(this);

    }
    /**
     * Method for adding a new FixedAccount object to the fixedAccounts Hashtable
     * @param f the FixedAccount object that needs to be added
     */
    public void addFixedAccount(FixedAccount f){
        this.fixedAccounts.put(f.getAccountNo(), f);
        f.customer.setFixedAccountNo(f.accountNo);
        new PersistenceManager().savedata(this);
    }
    /**
     * Method for adding a new CurrentAccount object to the currentAccounts Hashtable
     * @param c the CurrentAccount object that needs to be added
     */
    public void addCurrentAccount(CurrentAccount c){
        this.currentAccounts.put(c.getAccountNo(), c);
        c.customer.setCurrentAccountNo(c.accountNo);
        new PersistenceManager().savedata(this);
    }
    /**
     * Method for adding a new SavingsAccount object to the savingsAccounts Hashtable
     * @param s the SavingsAccount object that needs to be added
     */
    public void addSavingsAccount(SavingsAccount s){
        this.savingsAccounts.put(s.getAccountNo(), s);
        s.customer.setSavingsAccountNo(s.accountNo);
        new PersistenceManager().savedata(this);
    }
    /**
     * Method for adding a new Loan object to the loans Hashtable
     * @param l the Loan object that needs to be added
     */
    public void addLoan(Loan l){
        this.loans.put(Integer.toString(l.getLoanNo()), l);
        l.getCustomer().setLoanNo(String.valueOf(l.getLoanNo()));
        new PersistenceManager().savedata(this);
    }
    /**
     * Method for adding a new Staff object to the staff Hashtable
     * @param s the Staff object that needs to be added
     */
    public void addStaff(Staff s){
        this.staff.put(s.getEmployeeID(), s);
        new PersistenceManager().savedata(this);
    }
}
